package com.apes.scm.masterdata.party.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.repository.PartySerialNumberRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.scm.masterdata.party.model
 * @ClassName: PartySerialNumber
 * @Author: pyb
 * @CreateTime: 2020-05-19 10:18
 * @Description: 公司维度自增编码流水号记录表
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "party_serial_number")
public class PartySerialNumber extends BaseModel {

    @Id
    @ApesGenerator(code = "party_serial_number_seq", name = "公司流水号序列")
    @Comment("编码")
    private String id;

    @Column(name = "name", nullable = false, length = 64)
    @Comment("维度名称")
    private String name;

    @Column(name = "company_id", nullable = false, columnDefinition = " number(6) default 0")
    @Comment("公司编码")
    private int companyId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "operation", referencedColumnName = "id")
    @Comment("运营模式编码")
    private Enumeration operation; //运营模式

    @Column(name = "dept_id", nullable = false, columnDefinition = " number(6) default 0")
    @Comment("部门编码")
    private int deptId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_type", referencedColumnName = "id")
    @Comment("部门类型")
    private Enumeration deptType;

    @Column(name = "depot_id", nullable = false, columnDefinition = " number(6) default 0")
    @Comment("逻辑仓库编码")
    private int depotId;

    @Column(name = "phyDepot_id", nullable = false, columnDefinition = " number(6) default 0")
    @Comment("物理仓库编码")
    private int phyDepotId;

    @Column(name = "location_id", nullable = false, columnDefinition = " number(6) default 0")
    @Comment("库区编码")
    private int locationId;

    /**
     * 检查并创建
     *
     * @param party
     * @return
     */
    public PartySerialNumber checkParty(Party party) {
        //获取流水号
        PartySerialNumber partySerialNumber = new PartySerialNumber();
        String byMaxCompanyId = SpringManager.getBean(PartySerialNumberRepository.class).findByMaxCompanyId(party.getCorporate().getOperation().getId());

        int companyId = Integer.parseInt(byMaxCompanyId == null ? "0" : byMaxCompanyId);
        if (companyId > 0) {
            partySerialNumber.setCompanyId(companyId + 1);
        } else {
            partySerialNumber.setCompanyId(party.getCorporate().getOperation().getId().equals("001071") ? 501 :
                    (party.getCorporate().getOperation().getId().equals("001072") ? 1 : 6001));
        }
        partySerialNumber.setName(party.getName());
        partySerialNumber.setOperation(party.getCorporate().getOperation());
        return SpringManager.getBean(PartySerialNumberRepository.class).saveAndFlush(partySerialNumber);
    }

}
